//
//  ZKViewCitySelect.swift
//  Swift-app开发基础架构
//
//  Created by 朱凯 on 2017/11/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

import UIKit

class KZCitySelectView: KZBombView {

    // 省市区数据数组（市区数组动态变化）
    private var provinces: [KZProvince]!
    private var citys: [KZCity]!
    private var areas: [KZArea]!
    
    // 记录当前选中的省市区行索引，用于回调
    private var selectedProvinceRow = 0
    private var selectedCityRow = 0
    private var selectedAreaRow = 0
    
    // 完成回调
    var completionClouse: ((KZProvince, KZCity, KZArea) -> ())?
    
    var container: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(white: 0.96, alpha: 1)
        view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 216 + 40)
        return view
        
    }()
    
    func initDataFromDB() {
        
        provinces = KZCityDataBase.queryAllProvinces()
        citys = KZCityDataBase.queryAllCitys(withProvinceID: provinces.first!.id)
        areas = KZCityDataBase.queryAllAreas(withCityID: citys.first!.id)
    }
    
    func initViews() {
        
        // 包含一个完成按钮和背景和分隔线
        let toolBar: UIView = UIView()
        container.addSubview(toolBar)
        toolBar.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.left.top.right.equalTo(container)
        }
        
        let button = UIButton(type: .custom)
        button.setTitle("完成", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.addTarget(self, action: #selector(completionAction), for: .touchUpInside)
        button.contentHorizontalAlignment = .right
        toolBar.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.height.equalTo(toolBar)
            make.width.equalTo(100)
            make.right.equalTo(-10)
            make.top.equalTo(0)
        }
        let line = UIView()
        line.backgroundColor = kLineColor
        toolBar.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.width.equalTo(toolBar)
            make.left.bottom.equalTo(toolBar)
            make.height.equalTo(1)
        }
        
        
        // 选择视图
        let picker = UIPickerView()
        picker.backgroundColor = UIColor.white
        picker.delegate = self
        picker.dataSource = self
        container.addSubview(picker)
        picker.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(container)
            make.top.equalTo(toolBar.snp.bottom)
        }
    }
    
    
    // MARK: UIPickerDelegate
    override func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    override func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return provinces.count
        }
        else if component == 1 {
            return citys.count
        }
        else {
            return areas.count
        }
    }
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        
        var text: String = ""
        if (component == 0) {
            text = provinces[row].name
        }
        else if (component == 1) {
            text = citys[row].name
        }
        else {
            text = areas[row].name
        }
        
        return text.attrubutesString(attrubutes: [NSAttributedStringKey.font: kFont(14)], scope: text)
    }
    override func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if (component == 0) {
            citys = KZCityDataBase.queryAllCitys(withProvinceID: provinces[row].id)
            areas = KZCityDataBase.queryAllAreas(withCityID: citys[0].id)
            
            selectedProvinceRow = row
            selectedCityRow = 0
            selectedAreaRow = 0
        }
        else if (component == 1) {
            areas = KZCityDataBase.queryAllAreas(withCityID: citys[row].id)
            
            selectedCityRow = row
            selectedAreaRow = 0
        }
        else {
            selectedAreaRow = 0
        }
        
        // 刷新picker
        pickerView.reloadAllComponents()
        pickerView.selectRow(selectedProvinceRow, inComponent: 0, animated: true)
        pickerView.selectRow(selectedCityRow, inComponent: 1, animated: true)
        pickerView.selectRow(selectedAreaRow, inComponent: 2, animated: true)
    }
    
    @objc func completionAction() {
        
        self.hide()
        if let clouse = completionClouse {
            clouse(provinces[selectedProvinceRow], citys[selectedCityRow], areas[selectedAreaRow])
        }
    }

    
    // MARK: 重写show方法，来创建内容视图，并调用super的show方法展示视图
    override func show() {
        
        initDataFromDB()
        initViews()
        
        contentView = container
        
        super.show()
    }

}
